package com.mmx.baseservice.system.service.impl;

import com.mmx.baseservice.common.mybatisplus.methods.CommonServiceImpl;
import com.mmx.baseservice.common.utils.CommUtil;
import com.mmx.baseservice.common.utils.PathUtil;
import com.mmx.baseservice.common.utils.UUIDUtil;
import com.mmx.baseservice.common.utils.returnbody.ReturnBody;
import com.mmx.baseservice.system.mapper.SysDbBackupMapper;
import com.mmx.baseservice.system.pojo.SysDbBackup;
import com.mmx.baseservice.system.service.SysDbBackupService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author mmx generator
 * @since 2021-02-05
 */
@Service
public class SysDbBackupServiceImpl extends CommonServiceImpl<SysDbBackupMapper, SysDbBackup> implements SysDbBackupService {

    @Value("${backup.mysql.ip:}")
    private String mysqlIp;
    @Value("${backup.mysql.host:}")
    private String mysqlHost;
    @Value("${backup.mysql.database:}")
    private String mysqlDatabase;
    @Value("${spring.datasource.druid.username}")
    private String mysqlUser;
    @Value("${spring.datasource.druid.password}")
    private String mysqlPassword;


    @Override
    public String add(String remark) {
        String folder = new SimpleDateFormat("yyyy_MM_dd").format(new Date());
        String basePath = "backup/" + folder + "/";
        String filePath = PathUtil.getFullRealPath() + basePath;
        File uploadDir = new File(filePath);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        String fileName = new SimpleDateFormat("hhmmss").format(new Date()) + "_" + UUIDUtil.getCode() + ".sql";
        String cmd = MessageFormat.format("cmd /c mysqldump -h{0} -P{1} -u{2} -p{3} {4} > {5}",
                mysqlIp, mysqlHost, mysqlUser, mysqlPassword, mysqlDatabase, filePath + fileName);

        try {
            boolean flag = CommUtil.runCmd(cmd);
            SysDbBackup sysSqlData = new SysDbBackup()
                    .setPath(basePath + fileName)
                    .setRemark(remark);
            if (flag) {
                if (!super.save(sysSqlData)) {
                    return ReturnBody.error();
                }
            } else {
                sysSqlData.setStatus(-1);
                if (!super.save(sysSqlData)) {
                    return ReturnBody.error();
                }
                return ReturnBody.error("数据备份失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            SysDbBackup sysSqlData = new SysDbBackup()
                    .setPath(basePath + fileName)
                    .setRemark(remark)
                    .setStatus(-1);
            if (!super.save(sysSqlData)) {
                return ReturnBody.error();
            }
            return ReturnBody.error("数据备份失败");
        }
        return ReturnBody.success("数据备份成功!!!");
    }
}
